//
//  YXMineNewAddressViewController.m
//  CreditCard
//
//  Created by 杨旭 on 2017/12/25.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "YXMineNewAddressViewController.h"
#import "YXMineNewsAddressView.h"
#import "YXCityChooseView.h"
#import "YXAddressModel.h"
@interface YXMineNewAddressViewController ()
@property (nonatomic ,strong) YXMineNewsAddressView *addressView;
/** 城市选择 */
@property (nonatomic ,strong) YXCityChooseView *cityChoose;
/** 省编号*/
@property (nonatomic ,strong) NSString *provinceID;
/** 市编号 */
@property (nonatomic ,strong) NSString *cityID;
/** 区编号 */
@property (nonatomic ,strong) NSString *townID;

@end

@implementation YXMineNewAddressViewController

- (YXMineNewsAddressView *)addressView {
    if (!_addressView) {
        _addressView = [[YXMineNewsAddressView alloc] initWithFrame:self.view.bounds];
        YXWeakSelf
        [_addressView setSelectAddressBlock:^{
            
            weakSelf.cityChoose = [[YXCityChooseView alloc] init];
            weakSelf.cityChoose.config = ^(NSString *province, NSString *city, NSString *town,NSString *provinceID ,NSString *cityID,NSString *townID) {
            weakSelf.addressView.addressLab.text = [NSString stringWithFormat:@"%@%@%@",province,city,town];
                
                weakSelf.provinceID = provinceID;
                weakSelf.cityID = cityID;
                weakSelf.townID = townID;
                
            };
            [weakSelf.view addSubview:weakSelf.cityChoose];
        }];
        
        [_addressView setAddAddressBlock:^(NSString *isChoice,NSString *title){
            
            if ([title isEqualToString:@"修改收货地址"]) {
                if ([weakSelf.addressView.addressLab.text isEqualToString:@"请选择"]) {
                    [weakSelf showTostMessage:@"请选择地址"];
                }else {
                    [weakSelf requestUpdateAddress:isChoice];
                }
            }else {
                if ([weakSelf.addressView.addressLab.text isEqualToString:@"请选择"]) {
                    [weakSelf showTostMessage:@"请选择地址"];
                }else {
                    [weakSelf requestData:isChoice];
                }
            }
          
          
        }];
    }
    return _addressView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self createLeftBarButtonItemWithImage:@"导航栏_002"];
   
    self.addressView.title = self.title;
    self.addressView.model = self.model;
    [self.view addSubview:self.addressView];
}

#pragma mark - 修改收货地址
- (void)requestUpdateAddress:(NSString *)isChoice {
    NSString *str = [self valiMobile:self.addressView.phoneTF.text];
    
    if(str) {
        [self showTostMessage:str];
    }else {
        
        //    http://121.196.210.134:8082/mpos/wx/updateAddress.do
        
//        NSString *urlStr = @"updateAddress.do";
//        YXUserInfoModel *model = [YXUserInfoManager getUserInfo];
//        NSDictionary *parameter = @{@"sessionId":model.sessionId,@"addresseeName":self.addressView.consigneeTF.text,@"phone":self.addressView.phoneTF.text,@"province":self.model.province,@"city":self.model.city,@"area":self.model.area,@"detailedAddress":self.addressView.addressTF.text,@"isChoice":isChoice == nil ? @"1" : isChoice,@"addressId":self.model.addressId};
        
        NSString *str = [[NSString stringWithFormat:@"updateAddress.do?sessionId=%@&addresseeName=%@&phone=%@&province=%@&city=%@&area=%@&detailedAddress=%@&isChoice=%@&addressId=%@", [YXUserInfoManager getUserInfo].sessionId, self.addressView.consigneeTF.text, self.addressView.phoneTF.text, self.model.province == nil ? @"" : self.model.province,self.model.city == nil ? @"" : self.model.city,self.model.area == nil ? @"" : self.model.area,self.addressView.addressTF.text,isChoice == nil ? @"1" : isChoice,self.model.addressId] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        
        [BX_NetWorkManager requestForGETWithUrl:str parameter:nil success:^(id responseObject) {
            if (REQUESTDATASUCCESS) {
                NSLog(@"%@",responseObject);
                
                [self showTostMessage:@"修改地址成功！"];
                
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    
                    [self leftBarBtnItemAction];
                });
                
                
            }
        } failure:^(NSError *error) {
            NSLog(@"%@",error);
            [self showTostMessage:@"修改地址失败！"];

        }];
    }
    
}

#pragma mark - 添加收货地址
- (void)requestData:(NSString *)isChoice {
    
    NSString *str = [self valiMobile:self.addressView.phoneTF.text];
    
    if(str) {
        [self showTostMessage:str];
    }else {
        //    http://121.196.210.134:8082/mpos/wx/addAddress.do
        
//        NSString *urlStr = @"addAddress.do";
//        YXUserInfoModel *model = [YXUserInfoManager getUserInfo];
//        NSDictionary *parameter = @{@"sessionId":model.sessionId,@"addresseeName":self.addressView.consigneeTF.text,@"phone":self.addressView.phoneTF.text,@"province":self.provinceID == nil ? @"" : self.provinceID,@"city":self.cityID == nil ? @"" : self.cityID,@"area":self.townID == nil ? @"" : self.townID,@"detailedAddress":self.addressView.addressTF.text,@"isChoice":isChoice == nil ? @"1" : isChoice};
        
        NSString *str = [[NSString stringWithFormat:@"addAddress.do?sessionId=%@&addresseeName=%@&phone=%@&province=%@&city=%@&area=%@&detailedAddress=%@&isChoice=%@", [YXUserInfoManager getUserInfo].sessionId, self.addressView.consigneeTF.text, self.addressView.phoneTF.text, self.provinceID == nil ? @"" : self.provinceID,self.cityID == nil ? @"" : self.cityID,self.townID == nil ? @"" : self.townID,self.addressView.addressTF.text,isChoice == nil ? @"1" : isChoice] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        
        
        [BX_NetWorkManager requestForGETWithUrl:str parameter:nil success:^(id responseObject) {
            if (REQUESTDATASUCCESS) {
                NSLog(@"%@",responseObject);
                
                [self showTostMessage:@"添加地址成功"];
                
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    
                    [self leftBarBtnItemAction];
                });
                
                
            }
        } failure:^(NSError *error) {
            NSLog(@"%@",error);
            [self showTostMessage:@"添加地址失败！"];

        }];
    }
    
}

@end
